package models

import "time"

type BCUCollect struct {
	Model
	ValueType string `json:"valueType" gorm:"value_type"`
	PointId int `json:"pointId" gorm:"point_id"`
	Value float64 `json:"value"`
	CollTime time.Time `json:"collTime" gorm:"coll_time"`
}

func GetBCUCollects(bcuId, duration int) []BCUCollect {
	var bcuCollects []BCUCollect
	var bcuVoltages []BCUCollect
	var bcuSocs []BCUCollect
	var bcuInsulations []BCUCollect
	db.Raw("select *,'Voltage' as value_type from bms_bcu_voltage where bcu_id=? and to_days(now())-to_days(coll_time)<=?", bcuId, duration).Scan(&bcuVoltages)
	bcuCollects = append(bcuCollects, bcuVoltages...)
	db.Raw("select *,'SOC' as value_type, 0 as point_id from bms_bcu_soc where bcu_id=? and to_days(now())-to_days(coll_time)<=?", bcuId, duration).Scan(&bcuSocs)
	bcuCollects = append(bcuCollects, bcuSocs...)
	db.Raw("select *,'Insulation' as value_type, 0 as point_id from bms_bcu_insulation where bcu_id=? and to_days(now())-to_days(coll_time)<=?", bcuId, duration).Scan(&bcuInsulations)
	bcuCollects = append(bcuCollects, bcuInsulations...)
	return bcuCollects
}

func GetBCUOutline(bcuId int) map[string]interface{} {
	data := make(map[string]interface{})
	var bcuCollect BCUCollect
	db.Table("bms_bcu_voltage").Select("value").Where("bcu_id=? and point_id=?", bcuId, 1).Order("coll_time desc").Take(&bcuCollect)
	data["B+"] = bcuCollect.Value
	db.Table("bms_bcu_voltage").Select("value").Where("bcu_id=? and point_id=?", bcuId, 2).Order("coll_time desc").Take(&bcuCollect)
	data["HV1"] = bcuCollect.Value
	db.Table("bms_bcu_voltage").Select("value").Where("bcu_id=? and point_id=?", bcuId, 3).Order("coll_time desc").Take(&bcuCollect)
	data["HV2"] = bcuCollect.Value
	db.Table("bms_bcu_soc").Select("value").Where("bcu_id=?", bcuId).Order("coll_time desc").Take(&bcuCollect)
	data["SOC"] = bcuCollect.Value
	db.Table("bms_bcu_insulation").Select("value").Where("bcu_id=?", bcuId).Order("coll_time desc").Take(&bcuCollect)
	data["insulation"] = bcuCollect.Value
	return data
}